package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.dao.LoginLogMapper;
import com.xiangyi.teachingsystem.entity.po.Logging;
import com.xiangyi.teachingsystem.entity.po.LoginLog;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.LoggingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LoggingController {
    @Autowired
    LoggingService loggingService;
    @Autowired
    LoginLogMapper loginLogMapper;

    @PostMapping("/uploadLog")
    public Result uploadLog(@Validated @RequestBody Logging logging){
        try {
            loggingService.save(logging);
            return Result.success("日志上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getLogs")
    public Result getLogs(@RequestParam Integer userId){
        try {
            List<Logging> loggings = loggingService.getBaseMapper().selectList(new QueryWrapper<Logging>()
                    .eq("user_id", userId)
                    .orderByDesc("gmt_create"));
            return Result.success("日志查询成功").setData(loggings);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/LoginLog")
    public Result LoginLog(@RequestParam Integer userId){
        List<LoginLog> loginLogs = loginLogMapper.selectList(new QueryWrapper<LoginLog>()
                .eq("user_id", userId)
                .orderByDesc("last_time"));
        return Result.success("登录日志查询成功").setData(loginLogs);

    }
}
